package com.java.basis.collection.set;

/**
 * @author wangning
 * @create 2021-04-22 21:43
 */
public class Person implements Comparable<Person> {
	private String name;
	private Integer age;

	public Person() {}

	public Person(String name, Integer age) {
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "Person{" +
				"name='" + name + '\'' +
				", age=" + age +
				'}';
	}

//	@Override
//	public boolean equals(Object o) {
//		if (this == o) return true;
//		if (!(o instanceof Person)) return false;
//		Person person = (Person) o;
//		return Objects.equals(name, person.name) &&
//				Objects.equals(age, person.age);
//	}
//
//	@Override
//	public int hashCode() {
//		return Objects.hash(name, age);
//	}

	@Override
	public int hashCode() {
		int n1 = this.name.hashCode();
		int n2 = this.age.hashCode();
		return n1 + n2;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (obj instanceof Person) {
			Person person = (Person) obj;
			if (this.name.equals(person.getName()) && this.age.equals(person.getAge())) {
				return true;
			}
		}
		return false;
	}

	@Override
	public int compareTo(Person o) {
		//先按照姓名比，在按照年龄比
		//比较规则，如果两个姓名一样，就比较年龄
		int n1 = this.getName().compareTo(o.getName());
		int n2 = this.age - o.getAge();

		return n1 == 0 ? n2 : n1;
	}
}
